package com.gurun.camera.device.util;
import android.content.Context;
import android.support.annotation.NonNull;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateTimeUtil {

    // 格式：年－月－日 小时：分钟：秒
    public static final String PATTERN_YMD_HMS_1 = "yyyy-MM-dd HH:mm:ss";

    // 格式：年－月－日 小时：分钟
    public static final String PATTERN_YMD_HM = "yyyy-MM-dd HH:mm";

    // 格式：年月日 小时分钟秒
    public static final String PATTERN_YMD_HMS_2 = "yyyyMMdd-HHmmss";

    // 格式：年－月－日
    public static final String PATTERN_YMD = "yyyy-MM-dd";

    // 格式：月－日
    public static final String PATTERN_MD = "MM-dd";

    // 格式：小时：分钟：秒
    public static final String PATTERN_HMS = "HH:mm:ss";

    //格式：年-月
    public static final String PATTERN_YM = "yyyy-MM";

    //格式：时-分
    public static final String PATTERN_HM = "HH:mm";

    public static int getYear(long time) {
        return getDateFieldValue(time, Calendar.YEAR);
    }

    public static int getMonth(long time) {
        return getDateFieldValue(time, Calendar.MONTH);
    }

    public static int getDayOfMonth(long time) {
        return getDateFieldValue(time, Calendar.DAY_OF_MONTH);
    }

    private static int getDateFieldValue(long time, int field) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        int value = calendar.get(field);
        return value;
    }

    public static int getHour(long time) {
        return getDateFieldValue(time, Calendar.HOUR_OF_DAY);
    }

    public static int getMinute(long time) {
        return getDateFieldValue(time, Calendar.MINUTE);
    }

    public static String getTimeStringDefaultPattern(long time) {
        return getTimeString(time, PATTERN_YMD_HMS_1);
    }

    public static String getTimeString(long time, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date date = new Date(time);
        return sdf.format(date);
    }


    public static long getTimeMillis(int year, int month, int day, int hour, int minute, int second) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, day, hour, minute, second);
        return calendar.getTimeInMillis();
    }

    @NonNull
    private static String getString(Context context, int id) {
        return context.getResources().getString(id);
    }

    public static long formatDate(int year, int month, int day) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        StringBuilder builder = new StringBuilder();
        builder.append(year);
        builder.append("-");
        if (month < 10) {
            builder.append(0);
        }
        builder.append(month);
        builder.append("-");
        if (day < 10) {
            builder.append(0);
        }
        builder.append(day);

        builder.append(" 00:00:00");
        try {
            return dateFormat.parse(builder.toString()).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
